'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 领取累计充值天数活动奖励 
 * @author agl
 */

var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var PlayerManager = require('../../../../libs/playerManager');
var payConf = require('../../../../config/payConf.js');
var genRewardService = require('../../../../services/genRewardService');
var rewardService = require('../../../../services/rewardService');
var configUtil = require('../../../../util/configUtil.js');
var constantUtil = require('../../../../util/constantUtil.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    // 累计充值钻石天数
    var pick_total_pay_day = msg.total_pay_day;

    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    var start_time = configUtil.getOpenGameTime();
    var end_time = start_time + constantUtil.value('open_service_time') * configUtil.second_per_day;
    async.series(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'build', 'soldier', 'bag', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var total_pay_day = role_model.total_pay_day;
                var pick_list = role_model.total_pay_day_picked;

                var now_time = moment().unix();
                if (now_time < start_time || end_time < now_time) {
                    return next(null, {
                        code: Code.ACTIVITY_OUT_OF_DATE
                    });
                }

                if (pick_total_pay_day > total_pay_day) {
                    return next(null, {
                        code: Code.PAY_NOT_ENOUGH_PAY
                    });
                }

                var pay_conf = payConf.getTotalPayDayConf(pick_total_pay_day);
                if (!pay_conf) {
                    // 分档无效
                    return next(null, {
                        code: Code.PAY_INVALID_REWARD_GRADE
                    });
                }

                // 添加到已领取奖励列表中
                if (!role_model.addToTotalPayDayPicked(pick_total_pay_day)) {
                    // 已领取过对应奖励
                    return next(null, {
                        code: Code.PAY_REWARD_ALREADY_PICKED
                    });
                }

                // 发放奖励
                reward_list = genRewardService.generateRewardList(pay_conf);
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_PAY, operationType.OPR_ACT_PAY_REWARD_TOTAL_PAY_DAY);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
